package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.RoundCheckbox
import com.promise.jokerdream.ui.components.MultiSelectHashtagBottomSheet
import com.promise.jokerdream.ui.components.SelectionRow
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.model.knight.KnightRewardConfig
import com.promise.jokerdream.database.entity.KnightMissionGameConfigEntity

/**
 * 侠客岛任务设置卡片
 */
@Composable
fun KnightMissionSettingCard(
    config: KnightMissionGameConfigEntity,
    onKnightMissionChange: (Boolean) -> Unit,
    onRewardsChange: (List<String>) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    var showBottomSheet by remember { mutableStateOf(false) }
    
    SettingCard(
        title = "侠客岛",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onKnightMissionChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    // 奖励选择区域
                    SelectionRow(
                        label = "选择奖励：",
                        selectedText = remember(config.selectedKnightRewards) {
                            val rewardsList = if (config.selectedKnightRewards.isEmpty()) emptyList() else config.selectedKnightRewards.split(",").map { it.trim() }
                            if (rewardsList.isEmpty()) "未选择" else "已选择${rewardsList.size}项"
                        },
                        onClick = { showBottomSheet = true }
                    )
                }
            }
        }
    )

    // 奖励选择底部弹窗
    if (showBottomSheet) {
        MultiSelectHashtagBottomSheet(
            title = "选择奖励",
            initialTags = remember(config.selectedKnightRewards) {
                if (config.selectedKnightRewards.isEmpty()) emptyList() else config.selectedKnightRewards.split(",").map { it.trim() }
            },
            placeholder = "输入奖励名称，回车添加",
            recommendedTags = KnightRewardConfig.MISSION_REWARDS,
            onCancel = { showBottomSheet = false },
            onComplete = { rewards ->
                onRewardsChange(rewards)
                showBottomSheet = false
            },
            onDismiss = { showBottomSheet = false }
        )
    }
}
